home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************************
- *
- * PDP-8 Simulation Program- utilities, dialogs and initialisation
- *
- * ©1992 Graham Cox. All Rights Reserved.
- *
- * Modification History:
- * 3/3/92 created from scratch.
- *
- *
- *
- *************************************************************************************/
-
- #include "PDPGlobalEqu.p"
-
- #include "PDPUtilities.proto.h"
-
- int ProcessIndex = 1;
-
-
- PDPRegHdl NewProcessor(void)
- {
- /* allocates storage for a new processor data structure, initialises its fields
- and returns a handle to it */
-
- PDPRegHdl temp;
-
- temp = (PDPRegHdl) NewHandle(sizeof(PDPRegisters));
- if (temp != NIL) {
- (*temp)->PC = 0;
- (*temp)->ACC = 0;
- (*temp)->CCR = 0x80;
- (*temp)->EAR = 0;
- SetRect(&(*temp)->markerLoc,0,0,0,0);
- }
- return(temp);
- }
-
-
- ResetProcessor(PDPRegHdl theCPU)
- {
- /* resets the CPU so that the PC contains zero. No other registers affected */
-
- if (theCPU != NIL)
- (*theCPU)->PC = 0;
- }
-
-
- HaltProcessor(PDPRegHdl theCPU)
- {
- /* sets the halt bit in the CCR. No other registers affected */
-
- if (theCPU != NIL)
- (*theCPU)->CCR |= HaltBit;
- }
-
-
- StartProcessor(PDPRegHdl theCPU)
- {
- /* clears the halt bit in the CCR. Nothing else affected */
-
- if (theCPU != NIL)
- (*theCPU)->CCR &= (0xFF - HaltBit);
- }
-
-
- PDPMemHdl NewPDPMemory(void)
- {
- /* returns a handle to a new block of simulated memory. This block will contain
- random data */
-
- return((PDPMemHdl) NewHandle(sizeof(PDPMemory)));
- }
-
-
- InitProcessWindow(WindowPtr theWindow)
- {
- /* given a window, this procedure creates space for a cpu, a memory record, and a
- process record, and sets the windows refcon field to contain its handle. The
- windowKind field is set to SimulatorKind. This associates the window with a
- process which is so much better than defining globals. */
-
- ProcessRecHdl theProcess;
- PDPMemHdl theMem;
- PDPRegHdl theCPU;
- PrefsRecHdl thePrefs;
- ControlHandle mScroll;
- Rect sBarRect;
- Str32 procName,wTit;
- GrafPtr savePort;
-
- if (theWindow != NIL) {
- theProcess = (ProcessRecHdl) NewHandle(sizeof(ProcessRec));
- if (theProcess != NIL) {
- theMem = NewPDPMemory();
- theCPU = NewProcessor();
- (*theProcess)->theMemory = theMem;
- (*theProcess)->theCPU = theCPU;
- (*theProcess)->processOwner = theWindow;
- thePrefs = (PrefsRecHdl)NewHandle(sizeof(PrefsRec));
- SetPrefDefaults(thePrefs);
- (*theProcess)->procPrefs = thePrefs;
- (*theProcess)->reserved2 = NIL;
- (*(WindowPeek)theWindow).windowKind = SimulatorKind;
-
- sBarRect = theWindow->portRect;
- sBarRect.left = sBarRect.right-15;
- sBarRect.right +=1;
- sBarRect.bottom -= 14;
- sBarRect.top--;
-
- mScroll = NewControl(theWindow,&sBarRect,"\p",TRUE,0,0,4095,16,NIL);
- (*theProcess)->memScroll = mScroll;
- (*theProcess)->memLocHeight = 20;
-
- NumToString(ProcessIndex,&procName);
- CopyString("\pPDP-8 Process ",&wTit);
- ConcatString(&wTit,&procName);
- SetWTitle(theWindow,&wTit);
-
- (*theProcess)->processID = ProcessIndex;
- ProcessIndex += 1;
-
- GetPort(&savePort);
- SetPort(theWindow);
- TextFont(0);
- TextSize(12);
- SetPort(savePort);
-
- }
- SetWRefCon(theWindow,(long)theProcess);
- ClearProcessMem(theWindow);
- }
- }
-
-
- int IsSimulator(WindowPtr theWindow)
- {
- /* returns true if windowkind is SimulatorKind, else false */
-
- return((theWindow != NIL) && ((*(WindowPeek)theWindow).windowKind == SimulatorKind));
- }
-
-
- ProcessRecHdl GetProcess(WindowPtr theWindow)
- {
- /* returns handle to process if window is simulator kind */
-
- if (IsSimulator(theWindow))
- return((ProcessRecHdl)GetWRefCon(theWindow));
- else
- return(NIL);
- }
-
-
- int GetProcessID(WindowPtr theWindow)
- {
- /* returns the ID number of the associated process */
-
- ProcessRecHdl theProc;
-
- theProc = GetProcess(theWindow);
- if (theProc != NIL)
- return((*theProc)->processID);
- else
- return(0);
- }
-
-
- PDPRegHdl GetCPU(WindowPtr theWindow)
- {
- /* returns associated CPU handle from window */
-
- ProcessRecHdl theProcess;
-
- theProcess = GetProcess(theWindow);
- if (theProcess != NIL)
- return((*theProcess)->theCPU);
- else
- return(NIL);
- }
-
-
- PDPMemHdl GetMemory(WindowPtr theWindow)
- {
- /* returns associated memory handle from window */
-
- ProcessRecHdl theProcess;
-
- theProcess = GetProcess(theWindow);
- if (theProcess != NIL)
- return((*theProcess)->theMemory);
- else
- return(NIL);
- }
-
-
- ControlHandle GetMemScrollbar(WindowPtr theWindow)
- {
- /* returns associated scrollbar handle from window */
-
- ProcessRecHdl theProcess;
-
- theProcess = GetProcess(theWindow);
- if (theProcess != NIL)
- return((*theProcess)->memScroll);
- else
- return(NIL);
- }
-
-
- PrefsRecHdl GetPrefs(WindowPtr theWindow)
- {
- /* returns associated preferences handle from window */
-
- ProcessRecHdl theProcess;
-
- theProcess = GetProcess(theWindow);
- if (theProcess != NIL)
- return((*theProcess)->procPrefs);
- else
- return(NIL);
- }
-
-
- NumToHexx(long theNum,Str255 *theString)
- {
- /* converts a number to Hexadecimal string */
-
- int sLength;
- unsigned long i,r;
- Ptr temp,s2;
- char revStr[32];
-
- sLength = 0;
- i = theNum;
- temp = &revStr[0];
-
- if (i==0)
- CopyString("\p0",theString);
- else {
- while (i>0) {
- r = i % 16;
- i /= 16;
- if (r>9)
- *temp = (r & 0xFF) + 0x37;
- else
- *temp = (r & 0xFF) + 0x30;
-
- temp++;
- sLength++;
- }
- *theString[0] = sLength;
- s2 = (Ptr)theString;
- s2++;
-
- while(sLength >0) {
- temp--;
- *s2 = *temp;
- s2++;
- sLength--;
- }
- }
- }
-
-
- NumToOctal(long theNum,Str255 *theString)
- {
- /* converts a number to Octal string */
-
- int sLength;
- unsigned long i,r;
- Ptr temp,s2;
- char revStr[32];
-
- sLength = 0;
- i = theNum;
- temp = &revStr[0];
-
- if (i==0)
- CopyString("\p0",theString);
- else {
- while (i>0) {
- r = i % 8;
- i /= 8;
-
- *temp = (r & 0xFF) + 0x30;
-
- temp++;
- sLength++;
- }
- *theString[0] = sLength;
- s2 = (Ptr)theString + 1;
-
- while(sLength >0) {
- temp--;
- *s2 = *temp;
- s2++;
- sLength--;
- }
- }
- }
-
-
- HexStringToNum(Str32 *theString,unsigned long *theNum)
- {
- /* converts a hexadecimal string to a number */
-
- int count,sLength;
- unsigned long rTotal,weight;
- Ptr sPtr;
- char ascii;
-
- rTotal = 0;
- sPtr = theString;
- sLength = *sPtr;
- sPtr += sLength;
- weight = 1;
-
- for (count = 0;count < sLength;count++) {
- ascii = *sPtr;
- if (ascii > 0x40)
- rTotal += (ascii - 0x37) * weight;
- else
- rTotal += (ascii - 0x30) * weight;
-
- sPtr--;
- weight *= 16;
- }
- *theNum = rTotal;
- }
-
-
- OctStringToNum(Str32 *theString,unsigned long *theNum)
- {
- /* converts an octal string to a number */
-
- int count,sLength;
- unsigned long rTotal,weight;
- Ptr sPtr;
- char ascii;
-
- rTotal = 0;
- sPtr = theString;
- sLength = *sPtr;
- sPtr += sLength;
- weight = 1;
-
- for (count = 0;count < sLength;count++) {
- ascii = *sPtr;
- rTotal += (ascii - 0x30) * weight;
-
- sPtr--;
- weight *= 8;
- }
- *theNum = rTotal;
- }
-
-
- #define PrefDialogID 512
- #define PDFastButton 3
- #define PDMediumButton 4
- #define PDSlowButton 5
-
- #define PDVectorEditField 8
- #define PDDecimalButton 10
- #define PDOctalButton 11
- #define PDHexButton 12
- #define PDBinaryButton 13
- #define PDGroupFrame1 14
- #define PDGroupFrame2 15
- #define PDShowEARButton 16
- #define PDShowMnemonics 17
- #define PDTrackPCButton 18
-
- pascal void FrameRectUserItem(DialogPtr theDialog,int theItem)
- {
- /* user item proc to draw frames in dialog boxes */
-
- int itemType;
- Handle itemHand;
- Rect itemBox;
-
- GetDItem(theDialog,theItem,&itemType,&itemHand,&itemBox);
- FrameRect(&itemBox);
- }
-
-
- PDPPreferences(PrefsRecHdl thePrefs)
- {
- /* sets up preferences data structure using dialog box */
-
- DialogPtr theDialog;
- int theItem,itemType,i;
- Handle itemHand;
- Rect itemBox;
-
- theDialog = GetNewDialog(PrefDialogID,NIL,(WindowPtr)-1L);
-
- if (theDialog != NIL) {
-
- SetUserItem(theDialog,PDGroupFrame1,&FrameRectUserItem);
- SetUserItem(theDialog,PDGroupFrame2,&FrameRectUserItem);
- PosDialog(theDialog);
- OutlineDItem(theDialog,1);
-
- SetPrefButtons(theDialog,thePrefs);
- SelIText(theDialog,PDVectorEditField,0,32767);
-
- theItem = 0;
-
- while (theItem == 0) {
- ModalDialog(NIL,&theItem);
-
- switch (theItem) {
- case ok:
- GetPrefButtons(theDialog,thePrefs);
- case cancel:
- break;
- case PDFastButton:
- case PDMediumButton:
- case PDSlowButton:
- for (i=PDFastButton;i<=PDSlowButton;i++) {
- GetDItem(theDialog,i,&itemType,&itemHand,&itemBox);
- if (i==theItem)
- SetCtlValue(itemHand,1);
- else
- SetCtlValue(itemHand,0);
- }
- theItem = 0;
- break;
- case PDDecimalButton:
- case PDOctalButton:
- case PDHexButton:
- case PDBinaryButton:
- for (i=PDDecimalButton;i<=PDBinaryButton;i++) {
- GetDItem(theDialog,i,&itemType,&itemHand,&itemBox);
- if (i==theItem)
- SetCtlValue(itemHand,1);
- else
- SetCtlValue(itemHand,0);
- }
- theItem = 0;
- break;
- case PDShowEARButton:
- case PDShowMnemonics:
- case PDTrackPCButton:
- GetDItem(theDialog,theItem,&itemType,&itemHand,&itemBox);
- SetCtlValue(itemHand,GetCtlValue(itemHand) ^1);
- theItem = 0;
- break;
- default:
- theItem = 0;
- break;
- }
- }
-
- DisposDialog(theDialog);
- }
- }
-
-
- SetPrefButtons(DialogPtr theDialog,PrefsRecHdl thePrefs)
- {
- /* uses fields in prefs data structure to set buttons in dialog */
- int itemType,dValue;
- Handle itemHand;
- Rect itemBox;
- Str32 startVal;
-
- if (thePrefs != NIL && theDialog != NIL) {
- dValue = (*thePrefs)->CPUSpeed;
- switch(dValue) {
- case MediumCPU:
- GetDItem(theDialog,PDMediumButton,&itemType,&itemHand,&itemBox);
- break;
- case SlowCPU:
- GetDItem(theDialog,PDSlowButton,&itemType,&itemHand,&itemBox);
- break;
- case FastCPU:
- default:
- GetDItem(theDialog,PDFastButton,&itemType,&itemHand,&itemBox);
- break;
- }
- SetCtlValue(itemHand,1);
- dValue = (*thePrefs)->NumberFormat;
- switch(dValue) {
- case Octal:
- GetDItem(theDialog,PDOctalButton,&itemType,&itemHand,&itemBox);
- break;
- case Hexadecimal:
- GetDItem(theDialog,PDHexButton,&itemType,&itemHand,&itemBox);
- break;
- case Decimal:
- default:
- GetDItem(theDialog,PDDecimalButton,&itemType,&itemHand,&itemBox);
- break;
- }
- SetCtlValue(itemHand,1);
- dValue = (*thePrefs)->DefaultStart;
- NumToString(dValue,&startVal);
- GetDItem(theDialog,PDVectorEditField,&itemType,&itemHand,&itemBox);
- SetIText(itemHand,&startVal);
- }
- }
-
-
- GetPrefButtons(DialogPtr theDialog,PrefsRecHdl thePrefs)
- {
- /* uses buttons in dialog to set fields in prefs data structure */
-
- int itemType,i;
- Handle itemHand;
- Rect itemBox;
- Str32 startVal;
- long vector;
-
- if (thePrefs != NIL && theDialog != NIL) {
-
- for (i=PDFastButton;i<=PDSlowButton;i++) {
- GetDItem(theDialog,i,&itemType,&itemHand,&itemBox);
- if (GetCtlValue(itemHand))
- break;
- }
- switch(i) {
- case PDMediumButton:
- (*thePrefs)->CPUSpeed = MediumCPU;
- break;
- case PDSlowButton:
- (*thePrefs)->CPUSpeed = SlowCPU;
- break;
- case PDFastButton:
- default:
- (*thePrefs)->CPUSpeed = FastCPU;
- break;
- }
- for (i=PDDecimalButton;i<=PDBinaryButton;i++) {
- GetDItem(theDialog,i,&itemType,&itemHand,&itemBox);
- if (GetCtlValue(itemHand))
- break;
- }
- switch(i) {
- case PDOctalButton:
- (*thePrefs)->NumberFormat = Octal;
- break;
- case PDHexButton:
- (*thePrefs)->NumberFormat = Hexadecimal;
- break;
- case PDDecimalButton:
- default:
- (*thePrefs)->NumberFormat = Decimal;
- break;
- }
- GetDItem(theDialog,PDVectorEditField,&itemType,&itemHand,&itemBox);
- GetIText(itemHand,&startVal);
- StringToNum(&startVal,&vector);
- (*thePrefs)->DefaultStart = LoWord(vector);
- }
- }
-
-
- SetPrefDefaults(PrefsRecHdl thePrefs)
- {
- /* sets the default preferences */
-
- if (thePrefs != NIL) {
- (*thePrefs)->CPUSpeed = FastCPU;
- (*thePrefs)->NumberFormat = Octal;
- (*thePrefs)->DefaultStart = 0;
- (*thePrefs)->cycleTime = TickCount();
- }
- }
-
-
- char PDPProcHalted(WindowPtr pW)
- {
- /* returns TRUE if processor halted, else FALSE */
-
- PDPRegHdl theCPU;
-
- if (IsSimulator(pW)) {
- theCPU = GetCPU(pW);
- if (theCPU != NIL)
- return((*theCPU)->CCR & HaltBit);
- }
- }
-
-
- DisposeProcessWindow(WindowPtr theWindow)
- {
- /* releases all storage related to a simulator window. */
- ProcessRecHdl theProcess;
- PDPMemHdl theMemory;
- PDPRegHdl theCPU;
- int aHit;
-
- if (IsSimulator(theWindow)) {
- SetIndParamText(128,7);
- aHit = xCautionAlert(GeneralAlertCancel,NIL);
- if (aHit == ok) {
- theProcess = GetProcess(theWindow);
- theMemory = GetMemory(theWindow);
- theCPU = GetCPU(theWindow);
- if (theProcess != NIL)
- DisposHandle(theProcess);
- if (theMemory != NIL)
- DisposHandle(theMemory);
- if (theCPU != NIL)
- DisposHandle(theCPU);
- DisposeWindow(theWindow);
- }
- }
- }
-
-
- StackWindows(void)
- {
- /* call for stacking menu command, stacks Windows from top left to bottom right.
- Current FrontWindow is topmost */
-
- WindowPeek theWindow,listHead;
- int hGlob,vGlob,nWindows;
- int mWidth,mHeight;
- GDHandle theMonitor;
- Rect monRect;
- short optionWasDown;
-
- SetWatchCursor();
- optionWasDown = OptionDown();
- hGlob = 1;
- vGlob = GetMBarHeight()+1;
- listHead = (WindowPeek)FrontWindow();
- nWindows = 0;
- theWindow = listHead;
-
- if (IsInColour()) {
- theMonitor = GetMainDevice();
- monRect = (**theMonitor).gdRect;
- }
- else
- monRect = screenBits.bounds;
-
- while (theWindow!=NIL) {
- if (IsSimulator(theWindow) && theWindow->visible)
- nWindows++;
-
- theWindow = theWindow->nextWindow;
- }
-
- hGlob+=5*nWindows;
- vGlob+=20*nWindows;
- mWidth = monRect.right-monRect.left-hGlob-3;
- if (mWidth < 200)
- mWidth = 200;
-
- mHeight = monRect.bottom-monRect.top-vGlob-3;
- if (mHeight < 100)
- mHeight = 100;
-
- theWindow = listHead;
-
- while(theWindow!=NIL) {
- if (IsSimulator(theWindow) && theWindow->visible) {
- MoveWindow(theWindow,hGlob,vGlob,FALSE);
- hGlob-=5;
- vGlob-=20;
- }
- theWindow = theWindow->nextWindow;
- }
- }
-
-
-
-
-
-